/*
 * Copyright 2022-2029 heshuixing(https://gitee.com/heshuixing)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mercury.ec.framework.air.core.client.impl.qunar.execute;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mercury.ec.framework.air.core.client.impl.qunar.dto.QunarCommonReqDTO;
import com.mercury.ec.framework.air.core.client.impl.qunar.dto.QunarCommonRespDTO;
import com.mercury.ec.framework.air.core.client.impl.qunar.util.QunarUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

import static com.mercury.ec.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.mercury.ec.module.air.enums.ErrorCodeConstants.AIR_QUNAR_API_CALL_EXCEPTION;

/**
 * 去哪儿接口执行器
 *
 * @author hesx
 */
@Slf4j
public class DefaultQunarAirExecute implements QunarAirExecute {

    @Override
    public <T> QunarCommonRespDTO<T> execute(String url, String token, String secret, QunarCommonReqDTO request, Class<T> clazz) {
        QunarCommonRespDTO res = null;
        Map<String, Object> params = new HashMap<>();
        long start = System.currentTimeMillis();
        try {
            ObjectMapper mapper = new ObjectMapper();
            params.put("token", token);
            params.put("tag", request.getTag());
            params.put("createTime", System.currentTimeMillis());
            params.put("params", mapper.writeValueAsString(request));
            params.put("sign", QunarUtils.getQunarSign(params, secret));
            String result = HttpUtil.post(url, params);
            log.info("QUNAR接口地址【{}】接口【{}】请求【{}】耗时【{}】原始响应【{}】", url, request.getTag(), params, System.currentTimeMillis() - start, result);
            if (StrUtil.isNotBlank(result)) {
                // 忽略未能识别的字段
                mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                JavaType javaType = mapper.getTypeFactory().constructParametrizedType(QunarCommonRespDTO.class, QunarCommonRespDTO.class, clazz);
                res = mapper.readValue(result, javaType);
                log.info("QUNAR接口地址【{}】接口【{}】请求【{}】耗时【{}】标准响应【{}】", url, request.getTag(), params, System.currentTimeMillis() - start, JSONUtil.toJsonPrettyStr(res));
                res.setBody(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("QUNAR接口地址【{}】接口【{}】请求【{}】耗时【{}】调用异常", url, request.getTag(), params, System.currentTimeMillis() - start, e);
            throw exception(AIR_QUNAR_API_CALL_EXCEPTION, request.getTag(), e.getMessage());
        }
        return res;
    }
}
